# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import requests
from itertools import product
from common.helper import Helper
from common.config import *
from urllib.parse import urlencode


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "安联住院宝保障计划(2019版)"

    # 生成数据
    def genProductInfo(self):
        plan = {
            "planA": {
                # 年龄段
                "year": ["0-5", "6-35", "36-49", "50-65"],
                # 保障期限
                "days": ["1y"],
                # 投保人数
                "people_count": ["1"],
                # 意外伤残/死亡
                "accident_death": ["100000"],
                # 疾病住院医疗
                "disease_hospital": ["10000"],
                # 意外医疗
                "accident_medical": ["10000"],
            },
            "planB": {
                # 年龄段
                "year": ["0-5", "6-35", "36-49", "50-65"],
                # 保障期限
                "days": ["1y"],
                # 投保人数
                "people_count": ["1"],
                # 意外伤残/死亡
                "accident_death": ["200000"],
                # 疾病住院医疗
                "disease_hospital": ["20000"],
                # 意外医疗
                "accident_medical": ["20000"],
            }
        }
        for k in plan:
            for v in product(plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["accident_death"],
                             plan[k]["disease_hospital"], plan[k]["accident_medical"]):
                data = Helper.formatProductInfo(plan[k], v)
                yield data

    # 执行爬虫
    def requestJson(self):
        for v in self.genProductInfo():
            params = '''
            BTDT0060000000019742_TextAge: 50Y-65Y
            BTDT0060000000019743_Period: 1Y
            BTDT0060000000019744_Plan: AZBP1(2019)
            BTDT0060000000019745_Occup: 1-4
            duty: 204901307001-100000,204901307002-10000,204901307003-10000
            productid: 204901307
            complicatedFlag: N
            channelSn: agent_pc
            userTrack: 
            pointExchangeFlag: N
            '''
            data = Helper.perfectJson(params)
            if v["year"] == "0-5":
                days = "60D-5Y"
            else:
                days = "-".join([day + "Y" for day in v["year"].split("-")])
            data["BTDT0060000000019742_TextAge"] = days
            data["duty"] = "204901307001-200000,204901307002-20000,204901307003-20000" if v[
                                                                                              "accident_death"] == "200000" else "204901307001-100000,204901307002-10000,204901307003-10000"
            payload = urlencode( data ).replace("+","")
            r = requests.post( url = domain[self.name]["url"],headers=domain[self.name]["header"],data=payload)
            if r.status_code == 200 and "productDiscountPrice" in r.text:
                price = r.json()["data"]["productDiscountPrice"]
                data = {
                    "name":self.name,
                    "company":"京东安联",
                    "price":price
                }
                data.update( v )
                Helper.mergeTable(self.name,self.mysql,data )
